/**
 * 
 */
package com.itboye.jmsfhz.data.repository;

import java.lang.reflect.Type;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.reflect.TypeToken;
import com.itboye.core.interfaces.ICompleteListener;
import com.itboye.core.utils.BaseErrorListener;
import com.itboye.core.utils.BaseSuccessReqListener;
import com.itboye.core.utils.ByJsonRequest.Builder;
import com.itboye.core.utils.HttpUtils;
import com.itboye.jmsfhz.data.interfaces.IUserRepository;
import com.itboye.jmsfhz.data.model.PersonDataBean;

//import com.itboye.core.utils.HttpUtils;

/**
 * @author hebidu
 * 
 */
public class UserRepository extends BaseNetRepository implements
		IUserRepository<PersonDataBean> {

	public static String LoginByPwd = "By_User_login";// 用户登陆（正常登录）
	public static String RegisterService = "By_User_register";// 注册账号
	public static String FindPwdByPhone = "BY_User_findPswByMobile";// 找回密码（通过手机号码找回）
	public static String UpdateUserInfo = "BY_User_update";// 修改用户信息
	// private HttpUtils utils;
	public static String BindPhoneNumber = "BY_User_bind";// 绑定手机号码
	public static String UpdatePwdByPwd = "BY_User_updatePsw";// 修改密码（通过原密码修改）
	public static String UpdateBindPhoneNumber = "BY_User_changePhone";// 修改绑定手机
	public static String LoginByVer = "By_User_update";// 修改呢称和邮箱
	String api = "100";

	public UserRepository() {

	}

	public UserRepository(ICompleteListener listener) {
		this.setListener(listener);
	}

	/*
	 * @see
	 * com.itboye.mengwu.data.interfaces.IUserRepository#register(java.lang.
	 * Object) 2016-4-21 hebidu
	 */
	@Override
	public void register(PersonDataBean model) {
		Type type = new TypeToken<String>() {
		}.getType();
		Map<String, Object> params = new HashMap<String, Object>();

		params.put("username", model.getUsername());
		params.put("password", model.getPassword());
//		params.put("reg_type", 3);
		params.put("code", model.getCode());
		// Builder<PersonDataBean> builder = new Builder<PersonDataBean>();

		// 目前最简化调用
		// START
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(RegisterService, "101",
						params, type)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	/*
	 * 登录
	 * 
	 * @see
	 * com.itboye.mengwu.data.interfaces.IUserRepository#login(java.lang.Object)
	 * 2016-4-21 hebidu
	 */

	@Override
	public void login(PersonDataBean model) {

		String apiVer = "103";
		Map<String, Object> params = new HashMap<String, Object>();

		if (model.getType() == 0) {
			params.put("username", model.getUsername());
			params.put("password", model.getPassword());
			params.put("role", model.getRole());

			// Builder<PersonDataBean> builder = new Builder<PersonDataBean>();

			// 目前最简化调用
			// START
			(new Builder<PersonDataBean>())
					.setTypeVerParamsAndReturnClass(LoginByPwd, apiVer, params,
							PersonDataBean.class)
					.requestListener(
							new BaseSuccessReqListener<PersonDataBean>(
									getListener()))
					.errorListener(new BaseErrorListener(getListener()))
					.desEncodeThenBuildAndSend();
		}

	}

	/*
	 * @see
	 * com.itboye.mengwu.data.interfaces.IBaseRepository#update(java.lang.Object
	 * ) 2016-4-21 hebidu
	 */
	@Override
	public void update(PersonDataBean model) {
		String apiVer = api;
		Map<String, Object> params = new HashMap<String, Object>();
		switch (model.getType()) {
		case 1:
			params.put("mobile", model.getUsername());
			params.put("psw", model.getPassword());
			// params.put("code", model.getYanzhengma());
			(new Builder<String>())
					.setTypeVerParamsAndReturnClass(FindPwdByPhone, apiVer,
							params, String.class)
					.requestListener(
							new BaseSuccessReqListener<String>(getListener()))
					.errorListener(new BaseErrorListener(getListener()))
					.desEncodeThenBuildAndSend();
			break;
		case 2:
			params.put("uid", model.getUid());
//			params.put("nickname", model.getNickname());
//			params.put("sex", model.getSex());
			(new Builder<String>())
					.setTypeVerParamsAndReturnClass(UpdateUserInfo, apiVer,
							params, String.class)
					.requestListener(
							new BaseSuccessReqListener<String>(getListener()))
					.errorListener(new BaseErrorListener(getListener()))
					.desEncodeThenBuildAndSend();
			break;
		case 3:// 通过手机号码更换绑定手机
			params.put("uid", model.getUid());
			params.put("password", model.getPassword());
			params.put("mobile", model.getMobile());
			// params.put("code", model.getYanzhengma());
			(new Builder<String>())
					.setTypeVerParamsAndReturnClass(UpdateBindPhoneNumber,
							apiVer, params, String.class)
					.requestListener(
							new BaseSuccessReqListener<String>(getListener()))
					.errorListener(new BaseErrorListener(getListener()))
					.desEncodeThenBuildAndSend();
		case 4:// 第一次绑定手机号码
			params.put("uid", model.getUid());
			// params.put("code", model.getYanzhengma());
			params.put("mobile", model.getMobile());
			params.put("password", model.getMobile());
			(new Builder<String>())
					.setTypeVerParamsAndReturnClass(BindPhoneNumber, apiVer,
							params, String.class)
					.requestListener(
							new BaseSuccessReqListener<String>(getListener()))
					.errorListener(new BaseErrorListener(getListener()))
					.desEncodeThenBuildAndSend();
			break;
		case 5:// 通过密码修改密码
			params.put("uid", model.getUid());
			params.put("old_psw", model.getPassword());
			// params.put("psw", model.getNewPwd());
			(new Builder<String>())
					.setTypeVerParamsAndReturnClass(UpdatePwdByPwd, apiVer,
							params, String.class)
					.requestListener(
							new BaseSuccessReqListener<String>(getListener()))
					.errorListener(new BaseErrorListener(getListener()))
					.desEncodeThenBuildAndSend();

		default:
			break;
		}

	}

	/*
	 * @see
	 * com.itboye.mengwu.data.interfaces.IBaseRepository#getInfo(java.lang.Object
	 * ) 2016-4-21 hebidu
	 */
	@Override
	public void getInfo(PersonDataBean model) {
		Type type = new TypeToken<PersonDataBean>() {
		}.getType();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid",model.getMember_model().getUid());
		params.put("nickname", model.getMember_model().getNickname());

		// 目前最简化调用
		// START
		(new Builder<PersonDataBean>())
				.setTypeVerParamsAndReturnClass(LoginByVer, "102",
						params, type)
				.requestListener(
						new BaseSuccessReqListener<PersonDataBean>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();

	}

	/*
	 * @see
	 * com.itboye.mengwu.data.interfaces.IBaseRepository#add(java.lang.Object)
	 * 2016-4-21 hebidu
	 */
	@Override
	public void add(PersonDataBean model) {

		throw new UndeclaredThrowableException(new Throwable("该方法未实现!"));

	}

	/*
	 * @see
	 * com.itboye.mengwu.data.interfaces.IBaseRepository#delete(java.lang.Object
	 * ) 2016-4-21 hebidu
	 */
	@Override
	public void delete(PersonDataBean model) {
		throw new UndeclaredThrowableException(new Throwable("该方法未实现!"));
	}

	/*
	 * @see
	 * com.itboye.mengwu.data.interfaces.IBaseRepository#query(java.lang.Object)
	 * 2016-4-21 hebidu
	 */
	@Override
	public List<PersonDataBean> query(PersonDataBean model) {
		throw new UndeclaredThrowableException(new Throwable("该方法未实现!"));
	}

	@Override
	public void getUpdate(PersonDataBean model) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<String>() {
		}.getType();
		Map<String, Object> params = new HashMap<String, Object>();

		params.put("email", model.getEmail());
		params.put("uid", model.getUid());

		// 目前最简化调用getNikNameUpdate
		// START
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(LoginByVer, "102",
						params, type)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void getNikNameUpdate(PersonDataBean model) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<String>() {
		}.getType();
		Map<String, Object> params = new HashMap<String, Object>();

		params.put("nickname", model.getNickname());
		params.put("uid", model.getUid());

		// 目前最简化调用getNikNameUpdate
		// START
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(LoginByVer, "102",
						params, type)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}
}
